import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)

import fdreadline

import os
import StringIO

def mock_read(s, sz):
    return s.read(sz)

class FdreadlineTest(unittest.TestCase):   
    def testMonoLine(self):
        buf = 'foo\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foo')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testMonoLine2(self):
        buf = 'fooo\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'fooo')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testAlmostEmpyLine(self):
        buf = '\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), '')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
    
    def testEmpyLine(self):
        buf = ''
        s = StringIO.StringIO(buf)
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testTwoLines(self):
        buf = 'foo\nbar\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foo')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'bar')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testThreeLines1(self):
        buf = 'foo\n\nbar\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foo')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), '')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'bar')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testThreeLines2(self):
        buf = '\nfoo\nbar\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), '')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foo')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'bar')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testThreeLines3(self):
        buf = '\nfoo\nbar\nbud'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), '')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foo')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'bar')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'bud')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        
    def testLongerLine(self):
        old_size = fdreadline.get_buffersize()
        fdreadline.set_buffersize(2)
        buf = '\nfoofoofoo\nbarbarbar\nloplop\n'
        s = StringIO.StringIO(buf)
        self.assertEquals(fdreadline.fdreadline(s, mock_read), '')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'foofoofoo')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'barbarbar')
        self.assertEquals(fdreadline.fdreadline(s, mock_read), 'loplop')
        self.assertRaises(fdreadline.EOF, fdreadline.fdreadline, s, mock_read)
        fdreadline.set_buffersize(old_size)
        
if __name__ == '__main__':
    unittest.main()